<template>
  <!-- 查询客户 -->
  <div>
    <TopFilter
      :list="topFilterList"
      :cols="3"
      labelWidth="80"
      @filterChange="changeHandle"
      @onCollapse="collapseHandle"
    ></TopFilter>

    <FilterTable
      ref="table"
      :datakey="'records'"
      selectionType="single"
      height="auto"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :isPagination="false"
      :onRowSelectChange="selectChangeHandle"
      :onColumnsChange="columns => this.columns = columns"
    ></FilterTable>
    <div slot="footer" align="center" style="margin-top:10px">
      <el-button @click="saveDialog" type="primary">确 定</el-button>
      <el-button @click="closeDialog">返 回</el-button>
    </div>
  </div>
</template>

<script>
import { queryPotentialById } from '@/api/potenCus/customer/driveManagement';
import { queryCustomerOne } from '@/api/retail/salesmanage/salesordermanage';
import { dictionary } from '@/utils/dictMixin';
export default {
  name: 'SearchName',
  mixins: [dictionary],
  data() {
    return {
      topFilterList: this.createTopFilters(),
      fetchParams: {},
      visible: false,
      dialogTitle: '',
      fetchApi: queryCustomerOne,
      columns: this.createTableColumns(),
      handleType: '',
      formData: {},
      conf: { search: '选择试驾客户' },
      selectedRow: null
    };
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      if (this.type === 'edit' || this.type === 'show') {
        // 更新接口需要的记录 ID
        this.recordId = this.rowData.basedataId;
        this.formList.forEach(x => {
          x.initialValue = this.rowData[x.fieldName];
        });
      }
    },
    selectChangeHandle(rows) {
      if (!rows.length) return;
      this.selectedRow = rows[0];
    },
    createTopFilters() {
      return [
        {
          type: 'INPUT',
          label: '证件号码/手机号码',
          fieldName: 'certificateNoAndPhone',
          labelWidth:'150',
          style: {'display':'block','width': '249px','margin-left':'110px'},
          placeholder: '请输入'
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '客户名称',
          dataIndex: 'customerName',
        },
        {
          title: '手机号码',
          dataIndex: 'mobilePhone',
        },
        {
          title: '身份证号',
          dataIndex: 'certificateNo',
        }
      ];
    },
    saveDialog() {
      console.log('转介绍选择客户',this.selectedRow)
      if(this.selectedRow){
        this.closeDialog();
        this.$emit('NAMEHandle',this.selectedRow)
      } else {
        this.$message.info('未选择转介绍客户')
      }
    },
    closeDialog(data) {
      this.$emit('close', false, data);
    },
    showHandler() {
      this.dialogTitle = `${this.conf[this.handleType]}`;
      this.visible = true;
    },
    changeHandle(val) {
      console.log(val, '没有val你搜索个p')
      if(!val.certificateNoAndPhone){
        this.fetchParams = {}
      } else {
        this.fetchParams = Object.assign({},{type: 1}, val);
      }
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    closeHandler(val, type) {
      this.visible = val;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    }
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  }
};
</script>

<style lang="less" scoped>
</style>
